//
//  LGBreathReportChartView.m
//  HeartDisease_YHJK
//
//  Created by 1234 on 2025/8/6.
//  训练报告-趋势图

#import "LGBreathReportChartView.h"
#import "LGDottedLineView.h"
#import "LGScrollView.h"
#define topMargin     viewPix(40)

#define leftMargin    viewPix(40)

#define lineHeight    viewPix(30)

#define scrollWidth   (Screen_W - viewPix(32) - viewPix(56))

#define scrollHeight  (lineHeight*5)

@interface LGBreathReportChartView() 
/**   */
@property (nonatomic , strong)LGScrollView *scrollView;
/**   */
@property (nonatomic , strong)UIButton *typeBtn;
/**   */
@property (nonatomic , strong)UILabel *maxLabel;
/**   */
@property (nonatomic , strong)UILabel *emptyLabel;
/**   */
@property (nonatomic , strong)NSMutableArray *yAxisArray;
/**   */
@property (nonatomic , strong)NSMutableArray *lineArray;
/**   */
@property (nonatomic , assign)NSInteger totalCount;
/**   */
@property (nonatomic , assign)CGFloat itemW;

@end

@implementation LGBreathReportChartView

- (instancetype)initWithTitle:(NSString *)title frame:(CGRect)frame{
    self = [super initWithTitle:title frame:frame];
    if (self) {
        self.itemW = scrollWidth/10.0;
        self.gradientLayer.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        [self createSubView];
    }
    return self;
}

-(void)setTypeTitle:(NSString *)typeTitle{
    _typeTitle = typeTitle;
    [self.typeBtn setTitle:typeTitle forState:UIControlStateNormal];
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    self.emptyLabel.hidden = (dataArray.count>0)?YES:NO;
    if (dataArray.count == 0) return;
    self.totalCount = dataArray.count;
    NSInteger count = (self.totalCount>15)?15:self.totalCount;
    self.itemW = scrollWidth/count;
    //设置Y轴的值
    NSInteger maxValue = 2;
    for (NSInteger i=0; i<dataArray.count; i++) {
        NSInteger value = ceil([dataArray[i] floatValue]);
        maxValue = MAX(maxValue, value);
    }
    CGFloat tempValue = (maxValue%4==0) ? maxValue : (maxValue/4 + 1)*4;
    maxValue = (maxValue>2)?tempValue:2;
    CGFloat segment = maxValue/4.0;
    for (NSInteger i=0; i<self.yAxisArray.count; i++) {
        UILabel *label = self.yAxisArray[i];
        label.text = [NSString stringWithFormat:@"%.1f",maxValue-segment*i];
    }
    
    //创建点和曲线
    NSMutableArray *pointArray = [NSMutableArray array];
    for (NSInteger i=0; i<dataArray.count; i++) {
        CGFloat pointY = scrollHeight-([dataArray[i] floatValue]/segment*lineHeight+lineHeight*0.5);
//        CGFloat pointY = scrollHeight-([dataArray[i] floatValue]/maxValue*lineHeight+lineHeight*0.5);
        UIButton *button = [UIButton buttonWithImage:@"呼吸训练-point" selectImage:@"呼吸训练-point"];
        button.bounds = CGRectMake(0, 0, viewPix(20), viewPix(20));
        button.center = CGPointMake(viewPix(10)+self.itemW*i, pointY);
        button.tag = 2000+i;
//        [button addTarget:self action:@selector(pointButtonTouch:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
//        [self.btnArray addObject:button];
        NSValue *point = [NSValue valueWithCGPoint:button.center];
        [pointArray addObject:point];
    }
    [self drawCurveLine:pointArray lineColor:@"#429BFF"];
    self.scrollView.contentSize = CGSizeMake(self.itemW * dataArray.count+viewPix(15), scrollHeight);
    for (LGDottedLineView *lineView in self.lineArray) {
        lineView.frame = CGRectMake(0, lineView.frame.origin.y, self.itemW * dataArray.count+viewPix(20), lineHeight);
    }
}

//画线
-(void)drawCurveLine:(NSArray *)arry lineColor:(NSString *)color{
    if(arry.count == 0) return;
    CGPoint startPoint = [[arry objectAtIndex:0] CGPointValue];
    CGPoint lastPoint;
    UIBezierPath *lineBeizer = [UIBezierPath bezierPath];
    [lineBeizer setLineWidth:2.0];
    [lineBeizer moveToPoint:startPoint];

    for (NSInteger i = 0;i < arry.count;i++ ) {
        if (i <arry.count-1 ) {
            CGPoint nowPoint = [[arry objectAtIndex:i] CGPointValue];
            CGPoint futPoint  = [[arry objectAtIndex:i+1] CGPointValue];
            [lineBeizer addCurveToPoint:futPoint controlPoint1:CGPointMake((nowPoint.x+futPoint.x)/2, nowPoint.y) controlPoint2:CGPointMake((nowPoint.x+futPoint.x)/2, futPoint.y)];
            if (i == arry.count-2) {
                [lineBeizer moveToPoint:futPoint];//添加连线
                lastPoint = futPoint;
            }
        }
    }
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = lineBeizer.CGPath;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor colorWithString:color].CGColor;
    shapeLayer.lineWidth = 2.0;
    [self.scrollView.layer addSublayer:shapeLayer];
//    [self.lineArray addObject:shapeLayer];
    for(NSInteger i=0; i<self.totalCount; i++){
        UIButton *button = [self viewWithTag:2000+i];
        [self.scrollView bringSubviewToFront:button];
        
//        UIButton *button2 = [self viewWithTag:3000+i];
//        [self.scrollView bringSubviewToFront:button2];
    }
}

-(void)createSubView{
    [self addSubview:self.scrollView];
    [self addSubview:self.typeBtn];
    self.yAxisArray = [NSMutableArray array];
    self.lineArray = [NSMutableArray array];
    NSArray *titleArray = @[@"160",@"120",@"80",@"40",@"0"];
    for (NSInteger i=0; i<titleArray.count; i++) {
        //Y轴标题
        UILabel *label = [UILabel lableWithText:titleArray[i] colorString:@"#333333" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
        label.frame = CGRectMake(0, topMargin+lineHeight*i, leftMargin, lineHeight);
        [self addSubview:label];
        [self.yAxisArray addObject:label];
        //虚线
        LGDottedLineView *lineView = [[LGDottedLineView alloc]initWithFrame:CGRectMake(0, lineHeight*i, self.itemW*30+viewPix(20), lineHeight)];
        [self.scrollView addSubview:lineView];
        [self.lineArray addObject:lineView];
    }
    [self addSubview:self.emptyLabel];

}

-(LGScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[LGScrollView alloc] initWithFrame:CGRectMake(leftMargin, topMargin, scrollWidth, scrollHeight)];
        _scrollView.contentSize = CGSizeMake(self.itemW*30+viewPix(20), scrollHeight);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = NO;
//        _scrollView.delegate = self;
    }
    return _scrollView;
}

-(UIButton *)typeBtn{
    if(!_typeBtn){
        _typeBtn = [UIButton buttonWithTitle:@"" titleFont:LGFont(16) textColor:@"#282828" imageName:@"呼吸训练-point"];
        _typeBtn.frame = CGRectMake(0, kMaxY(self.scrollView.frame)-viewPix(5), Screen_W-viewPix(56), viewPix(25));
        _typeBtn.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(4), 0, 0);
        _typeBtn.userInteractionEnabled = NO;
    }
    return _typeBtn;
}


-(UILabel *)emptyLabel{
    if(!_emptyLabel){
        _emptyLabel = [UILabel lableWithText:@"暂无数据" colorString:@"#666666" textFont:LGFont(15) textAlignment:NSTextAlignmentCenter lines:1];
        _emptyLabel.frame = CGRectMake(0, topMargin, Screen_W-viewPix(32), scrollHeight+viewPix(30));
        _emptyLabel.backgroundColor = [UIColor whiteColor];
    }
    return _emptyLabel;
}
@end
